using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace CoolStuffs.Sharepoint.Webparts.RecentItems.Support
{
    /// <summary>
    /// This is the helper class
    /// and is needed to Group the Items
    /// in Code
    /// </summary>
    public class DataSetHelper
    {
        #region Nested Enums/Structures/Classes

        #region Public
        #endregion

        #region Protected
        #endregion

        #region Private
        #endregion

        #region Internal
        #endregion

        #endregion

        #region Member Variables

        #region Public
        #endregion

        #region Protected
        #endregion

        #region Private
        /// <summary>
        /// private dataset variable
        /// </summary>
        private DataSet _dataSetHelper;
        #endregion

        #region Internal
        #endregion

        #endregion

        #region Constructors/Destructors

        #region Public
        /// <summary>
        /// Paraterized Constructor
        /// </summary>
        /// <param name="DataSet">DataSet to set reference to</param>
        public DataSetHelper(ref DataSet DataSet)
        {
            this._dataSetHelper = DataSet;
        }

        /// <summary>
        /// Default Constructor
        /// </summary>
        public DataSetHelper()
        {
            this._dataSetHelper = null;
        }
        #endregion

        #region Protected
        #endregion

        #region Private
        #endregion

        #region Internal
        #endregion

        #endregion

        #region Properties

        #region Public
        /// <summary>
        /// get/set dataset
        /// </summary>
        public DataSet DatasetHelper
        {
            get
            {
                return this._dataSetHelper;
            }
            set
            {
                this._dataSetHelper = value;
            }
        }

        #endregion

        #region Protected
        #endregion

        #region Private
        #endregion

        #region Internal
        #endregion

        #endregion

        #region Methods

        #region Public
        /// <summary>
        /// Gets the DataTable with Distinct Values of
        /// specified column
        /// </summary>
        /// <param name="TableName">Name of the Table</param>
        /// <param name="SourceTable">Table to get distinct column values from</param>
        /// <param name="FieldName">Column name to perform DISTINCT on</param>
        /// <returns>DataTable with Distinct Values</returns>
        public DataTable SelectDistinct(string tableName, DataTable sourceTable, string fieldName)
        {
            DataTable _dataTable = new DataTable(tableName);
            _dataTable.Columns.Add(fieldName, sourceTable.Columns[fieldName].DataType);

            object _lastValue = null;
            foreach (DataRow dr in sourceTable.Select(string.Empty, fieldName))
            {
                if (_lastValue == null || !(columnEqual(_lastValue, dr[fieldName])))
                {
                    _lastValue = dr[fieldName];
                    _dataTable.Rows.Add(new object[] { _lastValue });
                }
            }
            if (_dataSetHelper != null)
                _dataSetHelper.Tables.Add(_dataTable);
            return _dataTable;
        }
        #endregion

        #region Protected
        #endregion

        #region Private
        /// <summary>
        /// Column Value Comparer
        /// </summary>
        /// <param name="valueA">First Value</param>
        /// <param name="valueB">Second Value</param>
        /// <returns></returns>
        private bool columnEqual(object valueA, object valueB)
        {
            // Compares two values to see if they are equal. Also compares DBNULL.Value.
            // Note: If your DataTable contains object fields, then you must extend this
            // function to handle them in a meaningful way if you intend to group on them.
            if (valueA == DBNull.Value && valueB == DBNull.Value) //  both are DBNull.Value
            {
                return true;
            }

            if (valueA == DBNull.Value || valueB == DBNull.Value) //  only one is DBNull.Value
            {
                return false;
            }

            return (valueA.Equals(valueB));  // value type standard comparison
        }
        #endregion

        #region Internal
        #endregion

        #endregion
    }
}
